package com.wf.demo.info.service;

import com.wf.demo.info.dao.RepairDAO;
import com.wf.demo.info.pojo.Device;
import com.wf.demo.info.pojo.Repair;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.job.pojo.DataDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RepairService {
    @Autowired
    RepairDAO repairDAO;
    @Autowired
    DeviceService deviceService;
    //查找单个设备所有维修记录
    public Page4Navigator<Repair> list(long did, int start, int size, int navigatePages){
        Device device=deviceService.get(did);
        Sort sort = new Sort(Sort.Direction.DESC, "id");//id倒序排序，最新的记录在最前面
        Pageable pageable = new PageRequest(start, size, sort);
        Page pageFromJPA = repairDAO.findByDevice(device,pageable);
        return new Page4Navigator<>(pageFromJPA,navigatePages);
    }
    //查找所有设备所有最新维修记录
    public Page4Navigator<Repair> list( int start, int size, int navigatePages){
        Sort sort = new Sort(Sort.Direction.DESC, "id");  //id倒序排序，最新的记录在最前面
        Pageable pageable = new PageRequest(start, size, sort);
        Page pageFromJPA = repairDAO.findAll(pageable);
        return new Page4Navigator<>(pageFromJPA,navigatePages);
    }
    //增删改查
    public void add(Repair bean){
        repairDAO.save(bean);
    }
    public void update(Repair bean){
        repairDAO.save(bean);
    }
    public void delete(Long id){
        repairDAO.delete(id);
    }
    public Repair get(Long id){
        return repairDAO.findOne(id);
    }

    //查找设备最新的repair集合，可指定有几个
    public List<Repair> getLatestRepaire (Device device, int size){
        Sort sort = new Sort(Sort.Direction.DESC, "id");  //id倒序排序，最新的记录在最前面
        Pageable pageable = new PageRequest(0, size, sort);
        Page<Repair> page= repairDAO.findByDevice(device,pageable);
        List<Repair> repairs =page.getContent();
        /*DataDevice d=dataDevices.get(0); //0位最新的一个
        System.out.println(d.getCreateTime());*/
        return  repairs;
    }


}
